package com.yhny.lykj.yuhannongye.app;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * BaseRefreshListAdapter
 * Created by LSC on 2015/7/14.
 */
public abstract class BaseRefreshListAdapter<VH extends BaseRefreshListAdapter.ViewHolder> extends RecyclerView.Adapter<BaseRefreshListAdapter.ViewHolder> implements Constants{

    public abstract void setOnItemClickListener(OnItemClickListener listener);



    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @Override
    public abstract int getItemCount();

    protected abstract void onBindBaseViewHolder(VH holder, int position);

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        onBindBaseViewHolder((VH) holder, position);
    }

    protected abstract int getLayoutResId(int viewType);

    protected abstract VH onCreateBaseViewHolder(View itemView);

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutResId(viewType), parent, false);
        return onCreateBaseViewHolder(view);
    }

    public abstract static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private OnItemClickListener listener;


        public ViewHolder(View itemView, OnItemClickListener listener) {
            super(itemView);
            this.listener = listener;

            if (listener != null) {
                itemView.setOnClickListener(this);
            }
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClick(getLayoutPosition());
            }
        }
    }
}
